/*
 * BasicPathTest.cpp
 *
 *  Created on: 17.01.2009
 *      Author: bfriedrich
 */
#include "StringAlgorithmTest.hpp"

#include <iostream>

CPPUNIT_TEST_SUITE_REGISTRATION(StringAlgorithmTest);

using namespace std;
using namespace boost;

void StringAlgorithmTest::testCaseConversion()
{
	const string HELLO_WORLD_BIG  ("HELLO WORLD!");
	const string HELLO_WORLD_SMALL("hello world!");
	const string HELLO_WORLD_TEST ("Hello World!");

    string testString(HELLO_WORLD_TEST);

    string upperString = to_upper_copy(testString);

    CPPUNIT_ASSERT(&testString != &upperString);
    CPPUNIT_ASSERT_EQUAL(testString,  HELLO_WORLD_TEST);
    CPPUNIT_ASSERT_EQUAL(upperString, HELLO_WORLD_BIG);


    string testString2(HELLO_WORLD_TEST);
    to_lower(testString2);

    CPPUNIT_ASSERT_EQUAL(testString2, HELLO_WORLD_SMALL);
}


void StringAlgorithmTest::testTrimming()
{
	const string HELLO_WORLD_BLANK_LEFT ("     Hello World!");
	const string HELLO_WORLD_BLANK_RIGHT("Hello World!     ");
	const string HELLO_WORLD_BLANK_BOTH ("    Hello World! ");
	const string HELLO_WORLD_NORMAL     ("Hello World!");

	//---------------------------------------------

	string testStringLeft(HELLO_WORLD_BLANK_LEFT);
	trim_left(testStringLeft);

	CPPUNIT_ASSERT_EQUAL(testStringLeft, HELLO_WORLD_NORMAL);

	//---------------------------------------------

	string testStringRight(HELLO_WORLD_BLANK_RIGHT);
	string newTestStringRight = trim_right_copy(testStringRight);

	CPPUNIT_ASSERT_EQUAL(testStringRight,    HELLO_WORLD_BLANK_RIGHT);
	CPPUNIT_ASSERT_EQUAL(newTestStringRight, HELLO_WORLD_NORMAL     );
	CPPUNIT_ASSERT      (&newTestStringRight != &testStringRight    );

	//----------------------------------------------

	string testStringBoth(HELLO_WORLD_BLANK_BOTH);

	trim(testStringBoth);
	CPPUNIT_ASSERT_EQUAL(testStringBoth, HELLO_WORLD_NORMAL);

	string testStringBothNumbers("1234" + HELLO_WORLD_NORMAL + "5678"); // --> "1234Hello World!5678"
	trim_if(testStringBothNumbers, is_digit());                         // --> "Hello World"

	CPPUNIT_ASSERT_EQUAL(testStringBothNumbers, HELLO_WORLD_NORMAL);
}


void StringAlgorithmTest::testPredicates()
{
	const string HELLO_WORLD_TEST ("Hello World!");

	// iXXXXX() bedeutet, dass Prüfung Case Insensitive ist
	CPPUNIT_ASSERT( starts_with(HELLO_WORLD_TEST, "Hello"));
	CPPUNIT_ASSERT(istarts_with(HELLO_WORLD_TEST, "hello"));
	CPPUNIT_ASSERT( ends_with  (HELLO_WORLD_TEST,  "!"   ));
	CPPUNIT_ASSERT( contains   (HELLO_WORLD_TEST,  "lo W"));
	CPPUNIT_ASSERT(icontains   (HELLO_WORLD_TEST,  "lo w"));
	CPPUNIT_ASSERT( equals     (HELLO_WORLD_TEST,  "Hello World!"));
	CPPUNIT_ASSERT(iequals     (HELLO_WORLD_TEST,  "hello world!"));

	//Achtung: lexicographical_compare() prüft, ob 1tes Argument > als das 2te ist
	CPPUNIT_ASSERT(!  lexicographical_compare( HELLO_WORLD_TEST, HELLO_WORLD_TEST));
	CPPUNIT_ASSERT(   lexicographical_compare( HELLO_WORLD_TEST, "hello world!"));
	CPPUNIT_ASSERT(! ilexicographical_compare( HELLO_WORLD_TEST, "hello world!"));

	//-- all() prüft, ob jede Stelle im übergebenen String dem Prädikat is_XXX() entspricht
	CPPUNIT_ASSERT(  all("12345",      is_digit() ));
	CPPUNIT_ASSERT(  all("helloworld", is_lower() ));
	// Aber:
	CPPUNIT_ASSERT(! all("hello world!", is_lower() )); // ab Boost 1.37 möglich
}


void StringAlgorithmTest::testEraseReplace()
{
	const string HELLO_WORLD_TEST ("Hello World!");

	//---------------------------------------

	string ciaoString(HELLO_WORLD_TEST + " Hello");
	replace_first(ciaoString, "Hello", "Bye");

	CPPUNIT_ASSERT(ciaoString == "Bye World! Hello");

	//---------------------------------------

	string helloString(HELLO_WORLD_TEST + "Hello");
	erase_last(helloString, "Hello");

	CPPUNIT_ASSERT(helloString == HELLO_WORLD_TEST);

	//----------------------------------------

	string anyString("Hello1_Hello2_Hello3 World");

	// Löscht das 2te Vorkommen von "Hello"
	erase_nth(anyString, "Hello", 1);
	CPPUNIT_ASSERT(anyString == "Hello1_2_Hello3 World");

	erase_all(anyString, "Hello");
	CPPUNIT_ASSERT(anyString == "1_2_3 World");
}
